﻿using NXDO.Data.Extension;
using NXDO.Data.Factory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TDM.DbData.DataEntity;

namespace TDM.WebBiz.TestData
{
    public class TestSubTaskHelper
    {

        public static TestSubTask Get(long id)
        {
            string sqltxt = "select * from testsubtask where staskid={0}";
            var rs = DbHelper.SetCommand(sqltxt, id).ExecuteEntityScalar<TestSubTask>();
            return rs;
        }
        public static void ExeCopySubTask(long sourceTaskID, int count, long uid)
        {
            //获取源子任务
            var sourceSubTask = Get(sourceTaskID);
            if (sourceSubTask == null) throw new Exception("未找到源子任务");
            var sourcePara = TestLaserParaHelper.GetBySTaskID(sourceTaskID);
            var sourceDatalst = TestLaserDataHelper.GetDatasBySTaskID(sourceTaskID).ToList();
            for (int i = 0; i < count; i++)
            {
                sourceSubTask.STaskID = 0;
                sourceSubTask.CreateTime = DateTime.Now;
                sourceSubTask.CreateUID = uid;
                sourceSubTask.DBLinq().ToCommand(sourceSubTask).Insert();

                if (sourcePara != null)
                {
                    TestLaserPara para = sourcePara.DBInstance().Clone();
                    para.STaskID = sourceSubTask.STaskID;
                    para.CreateTime = DateTime.Now;
                    para.CreateUID = uid;
                    para.LaserParaID = 0;
                    para.DBLinq().ToCommand(para).Insert();
                }
                for (int j = 0; j < sourceDatalst.Count; j++)
                {
                    TestLaserData data = sourceDatalst[j].DBInstance().Clone();
                    data.STaskID = sourceSubTask.STaskID;
                    data.CreateTime = DateTime.Now;
                    data.CreateUID = uid;
                    data.LaserDID = 0;
                    data.DBLinq().ToCommand(data).Insert();
                }
            }
        }

        public static IEnumerable<TestSubTask> GetTestSubTasks(long taskid)
        {
            string sqltxt = "select * from TestSubTask where taskid={0} and isdelete=0";
            var rs = DbHelper.SetCommand(sqltxt, taskid).ExecuteEntity<TestSubTask>();
            return rs;
        }
    }
}
